package com.example.bober.zad3;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.DocumentsContract;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.w3c.dom.Text;

import java.io.IOException;
import java.net.URL;


public class MyActivity extends Activity {

    EditText editText;
    TextView textView;
    Spinner spinner;
    String wal;
    Document doc;

    public void przelicz(View view) {
        try {
            String url = String.format("https://blockchain.info/pl/tobtc?currency=%s&value=%s", wal, editText.getText().toString());
            Log.d("url", url);
            (new ParseURL()).execute(new String[]{url});
            //doc = Jsoup.connect(url).get();
        } catch (final NumberFormatException e) {
            Toast.makeText(getApplicationContext(), "musisz podac liczbe calkowita", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        editText = (EditText) findViewById(R.id.wartosc);
        textView = (TextView) findViewById(R.id.wynik);
        spinner = (Spinner) findViewById(R.id.waluta);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.waluty, android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //Toast.makeText(getApplicationContext(), "Wybrano opcję" + (id + 1), Toast.LENGTH_SHORT).show();
                switch ((int) id) {
                    case 0: {
                        Log.d("waluta", "USD");
                        wal = "USD";
                        break;
                    }
                    case 1: {
                        Log.d("waluta", "JPY");
                        wal = "JPY";
                        break;
                    }
                    case 2: {
                        Log.d("waluta", "CNY");
                        wal = "CNY";
                        break;
                    }
                    case 3: {
                        Log.d("waluta", "SGD");
                        wal = "SGD";
                        break;
                    }
                    case 4: {
                        Log.d("waluta", "HKD");
                        wal = "HKD";
                        break;
                    }
                    case 5: {
                        Log.d("waluta", "CAD");
                        wal = "CAD";
                        break;
                    }
                    case 6: {
                        Log.d("waluta", "NZD");
                        wal = "NZD";
                        break;
                    }
                    case 7: {
                        Log.d("waluta", "AUD");
                        wal = "AUD";
                        break;
                    }
                    case 8: {
                        Log.d("waluta", "CLP");
                        wal = "CLP";
                        break;
                    }
                    case 9: {
                        Log.d("waluta", "GBP");
                        wal = "GBP";
                        break;
                    }
                    case 10: {
                        Log.d("waluta", "DKK");
                        wal = "DKK";
                        break;
                    }
                    case 11: {
                        Log.d("waluta", "SEK");
                        wal = "SEK";
                        break;
                    }
                    case 12: {
                        Log.d("waluta", "ISK");
                        wal = "ISK";
                        break;
                    }
                    case 13: {
                        Log.d("waluta", "CHF");
                        wal = "CHF";
                        break;
                    }
                    case 14: {
                        Log.d("waluta", "BRL");
                        wal = "BRL";
                        break;
                    }
                    case 15: {
                        Log.d("waluta", "EUR");
                        wal = "EUR";
                        break;
                    }
                    case 16: {
                        Log.d("waluta", "RUB");
                        wal = "RUB";
                        break;
                    }
                    case 17: {
                        Log.d("waluta", "PLN");
                        wal = "PLN";
                        break;
                    }
                    case 18: {
                        Log.d("waluta", "THB");
                        wal = "THB";
                        break;
                    }
                    case 19: {
                        Log.d("waluta", "KRW");
                        wal = "KRW";
                        break;
                    }
                    case 20: {
                        Log.d("waluta", "TWD");
                        wal = "TWD";
                        break;
                    }
                }


            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private class ParseURL extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... strings) {
            StringBuffer buffer = new StringBuffer();
            try {
                Log.d("JSwa", "Connecting to [" + strings[0] + "]");
                Document doc = Jsoup.connect(strings[0]).get();
                Log.d("JSwa", "Connected to [" + strings[0] + "]");
                Elements links = doc.getElementsByTag("body");
                String t = links.text();
                Log.d("body", t);
                //textView.setText(t);
                buffer.append(t);

            } catch (Throwable t) {
                t.printStackTrace();
            }

            return buffer.toString();
        }


        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            textView.setText(s);
        }
        //- See more at: http://www.survivingwithandroid.com/2014/04/parsing-html-in-android-with-jsoup.html#sthash.1wm8PCwf.dpuf
    }
}
